package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.view.*;
import com.glink.manage.service.ConstructionWorkSiteService;
import com.glink.manage.service.HomeSetService;
import com.glink.manage.service.InformationOverviewService;
import com.glink.manage.vo.attendance.AttendanceProjectStatisticsVO;
import com.glink.manage.vo.overview.HomeSetInfoVO;
import com.glink.manage.vo.video.PatrolIntelligentEquipChannelNvrVO;
import com.glink.manage.vo.view.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author  lumm
 * @since  2025-01-21
 */
@Slf4j
@RestController
@Api(tags = "信息总览")
@RequestMapping("/API/Manage/informationOverview")
public class InformationOverviewController  extends AbstractBaseController {

    @Autowired
    private InformationOverviewService informationOverviewService;

    @Autowired
    private ConstructionWorkSiteService constructionWorkSiteService;

    @Autowired
    private HomeSetService homeSetService;

    @ApiOperation(value = "1.施工现场大屏-根据施工现场ID查询参建单位的人员今日出勤、培训合格、特种作业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/queryOnSitePagePersonnelById",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ParticipatingUnitVO>> queryOnSitePagePersonnelById(@RequestBody SitePersonPageDTO sitePersonPageDTO) throws GeneralSimpleException {
        log.info("1.根据施工现场ID查询参建单位的人员今日出勤、培训合格、特种作业信息，dto:{}", LogUtil.encode(sitePersonPageDTO));

        // 校验授权
        constructionWorkSiteService.checkGrantByObjId(sitePersonPageDTO.getConstructionWorkSiteObjId());
        
        return ResponseResult.successResult(informationOverviewService.queryOnSitePagePersonnelById(sitePersonPageDTO));
    }

    @ApiOperation(value = "2.施工现场大屏-根据施工现场ID查询车辆出入信息")
    @PostMapping(value = "/queryVehicleAccessInfoById",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<VehicleAccessInfoVO> queryVehicleAccessInfoById(@RequestBody SitePersonPageDTO sitePersonPageDTO) throws GeneralSimpleException {
        log.info("2.根据施工现场ID查询车辆出入信息，dto:{}", LogUtil.encode(sitePersonPageDTO));

        // 校验授权
        constructionWorkSiteService.checkGrantByObjId(sitePersonPageDTO.getConstructionWorkSiteObjId());
        
        VehicleAccessInfoVO vehicleAccessInfoVO = informationOverviewService.queryVehicleAccessInfoById(sitePersonPageDTO);
        return ResponseResult.successResult(vehicleAccessInfoVO);
    }

    @ApiOperation(value = "3.施工现场大屏-保存施工现场和选择的摄像机信息")
    @PostMapping(value = "/saveWorkSiteAndDeviceInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> saveWorkSiteAndDeviceInfo(@RequestBody  HomeSetInfoDTO dto) throws GeneralSimpleException {
        log.info("3.保存施工现场和选择的摄像机信息，dto:{}", LogUtil.encode(dto));
        // 校验授权
        constructionWorkSiteService.checkGrantByObjId(dto.getConstructionWorkSiteId());
        return ResponseResult.successResult(homeSetService.insertHomeSet(dto));
    }

    @ApiOperation(value = "4.施工现场大屏-查询默认保存的施工现场及摄像机信息")
    @PostMapping(value = "/getHomeSetInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<HomeSetInfoVO> getHomeSetInfo(@RequestBody  HomeInfoGetInfoDTO dto) throws GeneralSimpleException {
        log.info("4.查询默认保存的施工现场及摄像机信息，dto:{}", LogUtil.encode(dto));
        return ResponseResult.successResult(homeSetService.getHomeSetInfo(dto));
    }


    @ApiOperation(value = "6.施工现场大屏-查询未添加到首页默认配置中的通道列表")
    @PostMapping(value = "/findNoHomeSetChannelListPage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<PatrolIntelligentEquipChannelNvrVO>> findNoHomeSetChannelListPage(@RequestBody @Validated PatrolIntelligentEquipChannelHomeDTO dto)throws GeneralSimpleException {
        log.info("6.查询未添加到首页默认配置中的通道列表：{}", LogUtil.encode(dto));
        // 校验授权
        constructionWorkSiteService.checkGrantByObjId(dto.getConstructionWorkSiteId());
        return this.response(homeSetService.findNoHomeSetChannelListPage(dto));
    }
 
    @ApiOperation(value = "1-1.总部-项目部大屏-查询工程概况信息")
    @GetMapping(value = "/queryProjectInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ConstructionWorkInfoVO>> queryProjectInfo() throws GeneralSimpleException {
        log.info("1-1.总部-项目部大屏-查询工程概况信息");
        return ResponseResult.successResult(informationOverviewService.queryProjectInfo());
    }

    @ApiOperation(value = "1-2.总部-项目部大屏-查询工程列表信息")
    @PostMapping(value = "/queryProjectList",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ConstructionWorkListVO>> queryProjectList(@RequestBody ConstructionStatusDTO dto) throws GeneralSimpleException {
        log.info("1-2.总部-项目部大屏-查询工程列表信息，dto:{}", LogUtil.encode(dto));
        return ResponseResult.successResult(informationOverviewService.queryProjectList(dto));
    }

    @ApiOperation(value = "1-3.总部-项目部大屏-查询考勤统计信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @GetMapping(value = "/queryAttendanceTally",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<AttendanceProjectStatisticsVO>> queryAttendanceTally() throws GeneralSimpleException {
        log.info("1-3.总部-项目部大屏-查询考勤统计信息");
        return ResponseResult.successResult(informationOverviewService.queryAttendanceTally());
    }

    @ApiOperation(value = "1-4.总部-项目部大屏-查询当日告警信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @GetMapping(value = "/queryAlarmInfoToday",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<AlarmInfoVO>> queryAlarmInfoToday() throws GeneralSimpleException {
        log.info("1-4.总部-项目部大屏-查询当日告警信息");
        return ResponseResult.successResult(informationOverviewService.queryAlarmInfoToday());
    }

//    @ApiOperation(value = "1-5.app-告警-查询工程下的告警信息")
//    @PostMapping(value = "/queryAlarmInfoByWorkId/{workId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public ResponseResult<List<AlarmInfoListVO>> queryAlarmInfoByWorkId(@PathVariable("workId")String workId) throws GeneralSimpleException {
//        log.info("1-5.app-告警-查询工程下的告警信息");
//        return ResponseResult.successResult(informationOverviewService.queryAlarmInfoByWorkId(workId));
//    }
    
    @ApiOperation(value = "5.施工现场大屏-查询天气")
    @PostMapping(value = "/queryWeather",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<SiteWeatherVO> queryWeather(@RequestBody @Validated SiteWeatherDTO dto) throws GeneralSimpleException {
        log.info("5.施工现场大屏-查询天气, {}", LogUtil.encode(dto));
        // 校验授权
        constructionWorkSiteService.checkGrantByObjId(dto.getConstructionWorkSiteObjId());
        
        SiteWeatherVO weatherVO = informationOverviewService.queryWeather(dto);
        log.info("5.施工现场大屏-查询天气, success");
        return ResponseResult.successResult(weatherVO);
    }

}
